
//--> Loth 2011 
//--> Jiglib engine init

package chamans.engine
{
	import flash.events.*;
	import flash.geom.*;
	
	import away3d.materials.ColorMaterial;
	import away3d.primitives.*;
	import away3d.entities.Mesh;
	
	import jiglib.cof.JConfig;
	import jiglib.geometry.*;
	import jiglib.math.*;
	import jiglib.physics.*;
	import jiglib.physics.constraint.*;
	import jiglib.plugin.AbstractPhysics;
	import jiglib.plugin.away3d4.*;
	
	public class Jiglib
	{
		private var BASE:Object;
		public var actif:Boolean=true;
		public var world:Away3D4Physics;
		private var p_ball:Vector.<RigidBody>;
		private var m_ball:Vector.<Mesh>;
		
		private var Steps:Number;
		private var times:Number;
		public var Minition:int=1;
		private var matBall:ColorMaterial;
		
		public function update():void
		{
			if(!actif)return;
			world.step(Steps);
		}
		public function Jiglib( base:Object, t:Number=10, s:Number=0.2 ):void
		{
			BASE=base; times=t; Steps=s;
			BASE.physicEngine="jiglib";
			config();
			world= new Away3D4Physics(BASE.view, times);
		}
		private function config():void
		{
			JConfig.solverType ="FAST" //"NORMAL"
			JConfig.doShockStep = true
			JConfig.numCollisionIterations = 1
			JConfig.numContactIterations = 1
			JConfig.collToll = 0.01
			JConfig.numPenetrationRelaxationTimesteps=times //10
		}
		public function info():String
		{
			return PhysicsSystem.getInstance().activeBodies.length+"|" + PhysicsSystem.getInstance().bodies.length;
		}
		public function add(n:*):RigidBody{
			var M:RigidBody=new JSphere(new Away3D4Mesh(n), 60) as RigidBody;
			return M
		}
		public function infini_plane():void
		{
			var mat:ColorMaterial=new ColorMaterial(0x00, 0)
			world.createGround(mat, 1,1,1,1,true,0)
		}
		public function add_Plan(f:Number=.6, r:Number=.6):void
		{
			var mat:ColorMaterial=new ColorMaterial(0x00, 0);
			var ground:Plane = new Plane(mat, 1, 1, 1, 1, true);
			var jGround:JPlane = new JPlane(new Away3D4Mesh(ground));
			jGround.friction=f;
			jGround.restitution=r;
			jGround.rotationX = 90;
			jGround.movable = false;
			world.addBody(jGround);
		}
		public function add_cube():void
		{
			var mat:ColorMaterial=new ColorMaterial(0xFF9090)
			mat.lights = BASE.LIGHT
			mat.specular = 1
			mat.gloss = 30
			var size:int= 50;
			var BOX : Cube = new Cube(mat,  size, size, size, 1,1,1, false);
			
			BASE.view.scene.addChild(BOX);
			var jBox:JBox = new JBox(new Away3D4Mesh(BOX), size, size, size);	
			jBox.moveTo(new Vector3D( 0, 100,0));
			jBox.mass = 10;
			world.addBody(jBox);
		}
		public function add_ball(v1:Vector3D, v2:Vector3D, v3:Vector3D, R:Vector3D):void
		{
			if(!matBall){
				matBall=new ColorMaterial(0xAAAAAA);
				matBall.lights = BASE.LIGHT;
				if(BASE.reflect)BASE.F_reflect(matBall, .6);
				if(BASE.fog)BASE.F_Fog(matBall);
				matBall.specular = 1;
				matBall.gloss = 30;
			}
			if(m_ball.length>10)del_ball();
			var size:int= 50;
			var body:Mesh;
			if(Minition==0)body = new Cube(matBall,50, 50, 50, 1,1,1, false);
			else body = new Sphere(matBall, 25, 30, 26);
			BASE.view.scene.addChild(body);
			
			var rigid:RigidBody;
			if(Minition==0)rigid = new JBox(new Away3D4Mesh(body), size, size, size);	
			else rigid = new JSphere(new Away3D4Mesh(body), 25);
			
			rigid.rotationX=R.x;
			rigid.rotationY=R.y;
			rigid.rotationZ=R.z;
			rigid.moveTo(v1);
			rigid.mass = 2;
			rigid.applyWorldImpulse(v3, v2);
			rigid.friction = .6;
			rigid.restitution = .6;
			world.addBody(rigid);
			p_ball.push(rigid);
			m_ball.push(body);
		}
		public function del_ball():void
		{
			while(m_ball.length>0){
				BASE.view.scene.removeChild(m_ball[m_ball.length-1])
				world.removeBody(p_ball[m_ball.length-1]);
				
				p_ball.pop()
				m_ball.pop()	
			}
		}
		public function activeGUN(B:Boolean=false) : void
		{
			if(B){BASE.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				p_ball = new Vector.<RigidBody>(); // hold all meshes with physic
				m_ball = new Vector.<Mesh>(); // hold all rigidbodies
			}
			else BASE.stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}	
		private function onMouseDown(event : MouseEvent) : void
		{
			if(!BASE.Camera.mouseback)return;
			// calculate impluse
			var pos : Vector3D 
			pos=BASE.view.camera.position.add(new Vector3D(0,-25,0))
			var mpos : Vector3D = BASE.Camera.target.position;
			var impulse : Vector3D = mpos.subtract(pos);
			impulse.normalize();
			impulse.scaleBy(400); 
			add_ball(pos, mpos,impulse, new Vector3D(-BASE.Camera.GUN.rotationX, BASE.Camera.GUN.rotationY, BASE.Camera.GUN.rotationZ))
		}
		//END
	}}